package com.hanxiaozhang.base.controller;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.hanxiaozhang.base.dao.JobTaskDao;
import com.hanxiaozhang.base.dto.JobTaskDTO;
import com.hanxiaozhang.base.quartz.config.QuartzHandler;
import com.hanxiaozhang.exception.InternalServerException;
import com.hanxiaozhang.result.Result;
import com.hanxiaozhang.util.PageUtil;
import com.hanxiaozhang.validation.util.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanxiaozhang.base.entity.JobTaskEntity;
import com.hanxiaozhang.base.service.JobTaskService;

import javax.annotation.Resource;


/**
 * 定时任务
 *
 * @author hanxiaozhang
 * @email hanxiaozhang2018@sina.com
 * @date 2021-04-29
 * @since 1.0.0
 */
@Slf4j
@Controller
@RequestMapping("/jobTask")
public class JobTaskController {

    @Autowired
    private JobTaskService jobTaskService;
    @Resource
    private JobTaskDao jobTaskDao;

    /**
     * 查询列表
     *
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('tool:jobTask:list')")
    public Result<PageUtil> list(@RequestParam Map<String, Object> params) {

        Page<JobTaskDTO> page = PageHelper
                .startPage(Integer.parseInt(params.get("current").toString()), Integer.parseInt(params.get("pageSize").toString()));
        List<JobTaskDTO> jobTaskList = jobTaskService.list(params);
        return Result.success(new PageUtil(jobTaskList, (int) page.getTotal()));

    }


    /**
     * 保存
     *
     * @param jobTask
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('tool:jobTask:save')")
    public Result<Void> save(JobTaskEntity jobTask) {

        valid(jobTask,true);
        jobTaskService.save(jobTask);
        return Result.success();
    }


    /**
     * 修改
     *
     * @param jobTask
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('tool:jobTask:update')")
    public Result<Void> update(JobTaskEntity jobTask) {

        valid(jobTask,false);
        jobTaskService.update(jobTask);
        return Result.success();
    }

    /**
     * 参数校验
     *
     * @param jobTask
     * @param saveFlag
     */
    private void valid(JobTaskEntity jobTask, boolean saveFlag) {

        ValidationUtil.checkParameter(ValidationUtil.checkObjectIsNotNull(jobTask.getJobName()), "任务名称不能为空");
        ValidationUtil.checkParameter(ValidationUtil.checkObjectIsNotNull(jobTask.getJobGroup()), "任务分组不能为空");
        ValidationUtil.checkParameter(ValidationUtil.checkObjectIsNotNull(jobTask.getCronExpression()), "cron表达式为空");
        ValidationUtil.checkParameter(ValidationUtil.checkObjectIsNotNull(jobTask.getBeanClass()), "全限定名不能为空");
        if (saveFlag) {
            JobTaskEntity byBeanClass = jobTaskDao.getByBeanClass(jobTask.getBeanClass());
            ValidationUtil.checkParameter(byBeanClass != null, "全限定名重复！");
        } else {
            JobTaskEntity byBeanClass = jobTaskDao.getByBeanClassNotId(jobTask.getBeanClass(), jobTask.getId());
            ValidationUtil.checkParameter(byBeanClass != null, "全限定名重复！");

        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    @PreAuthorize("hasAuthority('tool:jobTask:remove')")
    public Result<Void> remove(Long id) {

        jobTaskService.remove(id);
        return Result.success();
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @PreAuthorize("hasAuthority('tool:jobTask:batchRemove')")
    public Result<Void> remove(Long[] ids) {

        jobTaskService.batchRemove(ids);
        return Result.success();
    }


    @PostMapping(value = "/updateStatus")
    @ResponseBody
    @PreAuthorize("hasAuthority('tool:jobTask:updateStatus')")
    public Result updateStatus(Long id, Integer jobStatus) {

        jobTaskService.updateStatus(id, jobStatus);
        return Result.success();
    }

}
